package com.lxmes.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lxmes.common.constant.UserConstants;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.base.domain.JcIteminfoClass;
import com.lxmes.base.service.IJcIteminfoClassService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 物料大类Controller
 * 
 * @author 张志煜
 * @date 2022-06-11
 */
@RestController
@RequestMapping("/base/class")
public class JcIteminfoClassController extends BaseController
{
    @Autowired
    private IJcIteminfoClassService jcIteminfoClassService;

    /**
     * 查询物料大类列表
     */
    @PreAuthorize("@ss.hasPermi('base:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcIteminfoClass jcIteminfoClass)
    {
        startPage();
        //jcIteminfoClass.setDeptId(SecurityUtils.getDeptId());
        List<JcIteminfoClass> list = jcIteminfoClassService.selectJcIteminfoClassList(jcIteminfoClass);
        return getDataTable(list);
    }

    /**
     * 导出物料大类列表
     */
    @PreAuthorize("@ss.hasPermi('base:class:export')")
    @Log(title = "物料大类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcIteminfoClass jcIteminfoClass)
    {    jcIteminfoClass.setDeptId(SecurityUtils.getDeptId());
        List<JcIteminfoClass> list = jcIteminfoClassService.selectJcIteminfoClassList(jcIteminfoClass);
        ExcelUtil<JcIteminfoClass> util = new ExcelUtil<JcIteminfoClass>(JcIteminfoClass.class);
        util.exportExcel(response, list, "物料大类数据");
    }

    @GetMapping(value = "/getListClass")
    public AjaxResult getListClass()
    {
        List<JcIteminfoClass> list = jcIteminfoClassService.selectJcIteminfoClassList(new JcIteminfoClass());
        return AjaxResult.success(list);
    }

    /**
     * 获取物料大类详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jcIteminfoClassService.selectJcIteminfoClassById(id));
    }

    /**
     * 新增物料大类
     */
    @PreAuthorize("@ss.hasPermi('base:class:add')")
    @Log(title = "物料大类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcIteminfoClass jcIteminfoClass)
    {
        jcIteminfoClass.setDeptId(SecurityUtils.getDeptId());
        //校验物料是否存在
        List<JcIteminfoClass> iteminfoClassList = jcIteminfoClassService.selectJcIteminfoClassList(jcIteminfoClass);
        if(iteminfoClassList!=null&&iteminfoClassList.size()>0){
            return AjaxResult.error("新增大类'" + jcIteminfoClass.getClassName() + "'失败，大类名称已存在");
        }
        return toAjax(jcIteminfoClassService.insertJcIteminfoClass(jcIteminfoClass));
    }

    /**
     * 修改物料大类
     */
    @PreAuthorize("@ss.hasPermi('base:class:edit')")
    @Log(title = "物料大类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcIteminfoClass jcIteminfoClass)
    {
        //校验类别是否存在
        List<JcIteminfoClass> iteminfoClassList = jcIteminfoClassService.selectJcIteminfoClassList(jcIteminfoClass);
      /*  if(iteminfoClassList!=null&&iteminfoClassList.size()>0){
            return AjaxResult.error("修改大类'" + jcIteminfoClass.getClassName() + "'失败，大类名称已存在");
        }*/
        return toAjax(jcIteminfoClassService.updateJcIteminfoClass(jcIteminfoClass));
    }

    /**
     * 删除物料大类
     */
    @PreAuthorize("@ss.hasPermi('base:class:remove')")
    @Log(title = "物料大类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jcIteminfoClassService.deleteJcIteminfoClassByIds(ids));
    }
}
